package com.woniu.Service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.Service.AccountService;
import com.woniu.entity.Account;
import com.woniu.mapper.AccountMapper;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: Gwj
 * @Date: 2024/7/1 16:12
 */
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
    @Resource
    private AccountMapper accountMapper;

    /**
     * 根据名字查询出用户 用于登录
     *
     * @param accountName
     * @return
     * @throws Exception
     */
    @Override
    public Account findAccountToLogin(String accountName) throws Exception {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        Account account = accountMapper.selectOneByExample(example);
        return account;
    }

    /**
     * 根据用户名查询用户信息
     *
     * @param accountName
     * @return
     * @throws Exception
     */
    @Override
    public Account findAccountByName(String accountName) throws Exception {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        Account account = accountMapper.selectOneByExample(example);
        return account;
    }

    /**
     * 修改个人密码时的重复验证
     *
     * @param accountName
     * @param oldPassword
     * @return
     * @throws Exception
     */
    @Override
    public Account checkPassword(String accountName, String oldPassword) throws Exception {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        c.andEqualTo("password", oldPassword);
        Account account = accountMapper.selectOneByExample(example);
        return account;
    }

    /**
     * 修改个人密码
     *
     * @param accountName
     * @param newPassword
     * @return
     * @throws Exception
     */
    @Override
    public int updatePassword(String accountName, String newPassword) throws Exception {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        Account account = accountMapper.selectOneByExample(example);
        account.setPassword(newPassword);
        return accountMapper.updateByExampleSelective(account, example);
    }

    /**
     * 登录的同时修改登陆时间
     *
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    public void upadateAccountLoginDate(Account account) throws Exception {
        account.setLoginDate(new Date());
        accountMapper.updateByPrimaryKeySelective(account);
    }

    /**
     * 根据用户id修改用户信息
     *
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "account", allEntries = true)
    public int updateAccountInfoById(Account account) throws Exception {
        int i = accountMapper.updateByPrimaryKeySelective(account);
        return i;
    }

    /**
     * 删除用户 逻辑删除
     *
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "account", allEntries = true)
    public int changeAccountIsDeleted(Account account) throws Exception {
        account.setIsDelete((byte) 1);
        return accountMapper.updateByPrimaryKeySelective(account);
    }

    /**
     * 用户新增方法
     *
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "account", allEntries = true)
    public int addAccount(Account account) throws Exception {
        int i = accountMapper.insertSelective(account);
        return i;
    }

    /**
     * 分页查询所有的用户
     *
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    @Cacheable(value = "account", key = "#root.methodName+#root.args[0]+#root.args[1]")
    public PageInfo<Account> findAccountByPage(int pageNum, int pageSize) throws Exception {
        Page<Account> page = PageHelper.startPage(pageNum, pageSize);
        accountMapper.selectAll();
        return page.toPageInfo();
    }

    /**
     * 根据名字模糊查询，并且分页
     *
     * @param pageNum
     * @param pageSize
     * @param accountName
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<Account> findAccountLikeName(int pageNum, int pageSize, String accountName,Byte isDelete, String accountType) throws Exception {
        Page<Account> page = PageHelper.startPage(pageNum, pageSize);
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andLike("accountName", "%" + accountName + "%");
        c.andEqualTo("isDelete", isDelete); // 0表示正常，1表示已删除的用户，2表示被举报的用户
        c.andEqualTo("accountType", accountType);
        accountMapper.selectByExample(example);
        return page.toPageInfo();
    }


    /**
     *
     * 新增时的重名验证
     *
     * @param accountName
     * @return
     * @throws Exception
     */
    @Override
    public Account checkAccountName(String accountName) throws Exception {
        Example example = new Example(Account.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("accountName", accountName);
        return accountMapper.selectOneByExample(example);
    }

    /**
     * 修改用户信息时的重名验证
     *
     * @param accountName
     * @param accountId
     * @return
     * @throws Exception
     */
    @Override
    public Account selectAccountByNameAndId(String accountName, Integer accountId) throws Exception {
        return accountMapper.selectAccountByNameAndId(accountName, accountId);
    }

    /**
     * 批量重置用户密码 重置成 111111
     *
     * @param list
     * @return
     * @throws Exception
     */
    @Override
    @CacheEvict(value = "account", allEntries = true)
    public int updateAccountPasswordByIds(List<Account> list) throws Exception {
        int i = accountMapper.updatePasswordByIds(list);
        return i;
    }
}
